package com.grape.utils;

import cn.hutool.core.util.ObjectUtil;
import com.grape.vo.UserVO;

/**
 * 类描述： 当前线程工具类
 *
 * @author  putao
 * @email   2628279194@qq.com
 */
public class ThreadLocalUtil {
    private ThreadLocalUtil() { }

    private final static ThreadLocal<UserVO> THREAD_LOCAL_FRONT_USER = new ThreadLocal<>();

    public static void set(UserVO userVo) {
        THREAD_LOCAL_FRONT_USER.set(userVo);
    }

    public static UserVO get() {
        return THREAD_LOCAL_FRONT_USER.get();
    }

    /**
     * 获取用户id
     * @return  用户id
     */
    public static String getUserId() {
        if (ObjectUtil.isNotNull(get()))  {
            return get().getId();
        }
        return null;
    }

    /**
     * 获取名字
     * @return 用户名 username
     */
    public static String getUsername() {
        if (ObjectUtil.isNotNull(get()))  {
            return get().getUsername();
        }
        return null;
    }

    public static void remove() {
        THREAD_LOCAL_FRONT_USER.remove();
    }
}
